home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / Random.h < prev    next >
Encoding:
Text File  |  1993-01-14  |  3.9 KB  |  132 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright © 1991-93 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Date: Tuesday, June 2, 1992 11:53:42
  5.   Revision comments are at the end of this file.
  6.   ---
  7.   TRandom is a stackbased utility class for random number generation.
  8.   TRandom.h contains the header file information for the class.
  9.   _________________________________________________________________________________________________________ */
  10.  
  11.  
  12. // Declare label for this header file
  13. #ifndef _RANDOM_
  14. #define _RANDOM_
  15.  
  16.  
  17. // HEADER FILES
  18. #ifndef _DTSCPLUSLIBRARY_
  19. #include "DTSCPlusLibrary.h"
  20. #endif
  21.  
  22. //    TOOLBOX INCLUDES
  23. #ifndef __TYPES__
  24. #include <Types.h>
  25. #endif
  26.  
  27. #ifndef __OSUTILS__
  28. #include <OSUtils.h>
  29. #endif
  30.  
  31. #ifndef __kQUICKDRAW__
  32. #include <Quickdraw.h>
  33. #endif
  34.  
  35. #ifndef __EVENTS__
  36. #include <Events.h>
  37. #endif
  38.  
  39. //     EXTERNAL FUNCTIONS
  40. extern "C"
  41. {
  42.     pascal short AsmRandom();
  43. }
  44.  
  45.  
  46. ;
  47.  
  48.  
  49. // _________________________________________________________________________________________________________ //
  50. //    TRandom Interface
  51. class TRandom
  52. // The Random number class will produce random numbers within a certain range
  53. // which could be specified. Compared with the Toolbox Random function 
  54. // TRandom is also able to swith between various alternate random algorithms
  55. // and the random generator could be cloned using copy constructors
  56. {
  57. public:
  58.     //    TYPEDEFS AND ENUMS
  59.     enum ERandType
  60.     {
  61.         kMACOS, kQUICK, kSHUFFLE, kPM
  62.     };                                            // selected random number generator
  63.  
  64.     //    CONSTRUCTORS & DESTRUCTORS
  65.     TRandom(ERandType = kMACOS,
  66.             long theSeed = 1,
  67.             unsigned short low = 0,
  68.             unsigned short high = 100);            // default constructor
  69.  
  70.     TRandom(const TRandom&);                    // copy constructor
  71.     TRandom& operator=(const TRandom&);            // assignment operator
  72.     virtual~ TRandom();                            // virtual destructor
  73.  
  74. protected:
  75.     Boolean IRandom(ERandType);                    // initialize needed class information
  76.  
  77. public:
  78.     //  MAIN INTERFACES
  79.     unsigned short Next();                        // get next random number
  80.     TRandom& ShuffleSeed();                        // set seed value
  81.     TRandom& SetRandomGenerator(ERandType);        // select random number algorithm
  82.  
  83.     //    PUBLIC ACCESSORS AND MUTATORS
  84.     long GetSeedValue() const;                    // get current seed value
  85.     TRandom& SetSeedValue(const long theSeed);    // get seed value and return a this pointer to a class
  86.     ERandType GetAlgorithmType() const;            // get currently used algoritm type
  87.  
  88. protected:
  89.     //    RANDOM GENERATOR ALGORITHMS/MEMBER FUNCTIONS
  90.     unsigned short MacRandom();                    // Toolbox Random function
  91.     unsigned short QuickRandom();                // quick assembly version
  92.     unsigned short ShuffleRandom();                // shuffle Toolbox random values
  93.     TRandom& InitShuffleRandom();                // initialize the shuffle algorithm
  94.     unsigned short ParkMiller();                // Park&Miller Random generator
  95.  
  96.  
  97. protected:
  98.     //    TYPEDEFS AND ENUMS (INTERNAL IMPLEMENTATION)
  99.     enum eSIZE
  100.     {
  101.         kSHUFFLETABLE = 128, kPM1 = 2836, kPM2 = 16807, k16BIT = 65536, kPM3 = 127773, kMSEED = 1618003398, eBIG = 1000000000, kACM_MAX = 2147483647
  102.     };
  103.  
  104.  
  105.     typedef unsigned short(TRandom::* RandGen)();// ptr to actual selected random routine
  106.  
  107.     //    FIELDS
  108.     RandGen fGenerator;                            // selected random generator function
  109.     ERandType fAlgorithm;                        // selected algorithm
  110.     unsigned short fLow;                        // lower limit of random number
  111.     unsigned short fHigh;                        // higher limit of random number
  112.     unsigned short fRange;                        // max size random number
  113.     long fSeed;                                    // random generator seed
  114.     Boolean fState;                                // current state of object, OK or BAD
  115.     unsigned short fPrevNum;                    // previous random number
  116.     unsigned short fShuffleBuf[kSHUFFLETABLE];    // buffer for the shuffle algorithm
  117. };
  118.  
  119.  
  120. #endif 
  121.  
  122. // _________________________________________________________________________________________________________ //
  123.  
  124.  
  125. /*    Change History (most recent last):
  126.   No        Init.    Date        Comment
  127.   1            khs        6/2/92        New file
  128.   2            khs        1/7/93        Cleanup
  129. */
  130.  
  131.  
  132.